package com.haofengsoft.scce;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.haofengsoft.platform.utils.EquipmentUtil;
import com.haofengsoft.platform.utils.PhoneUtil;
import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.UserController;

/**
 * 登录
 *
 * @author zhj
 */
public class LoginActivity extends Activity {

    private EditText telEt;// 手机号
    private EditText pwdEt;// 密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        AppManage.getInstance().addActivity(LoginActivity.this);
        ImageView backIv = (ImageView) findViewById(R.id.login_back_iv);
        backIv.setOnClickListener(new BackClickListener());
        TextView registerTv = (TextView) findViewById(R.id.login_regist_btn);
        registerTv.setOnClickListener(new RegistClickListener());
        CheckBox expressPwdCb = (CheckBox) findViewById(R.id.login_password_express_cb);
        telEt = (EditText) findViewById(R.id.login_tel_et);
        pwdEt = (EditText) findViewById(R.id.login_password_et);
        expressPwdCb.setOnCheckedChangeListener(new PassWordOnCheckedChangeListener());
        Button loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(new LoginClickListener());

        TextView forgetPassTv = (TextView) findViewById(R.id.login_forgetpass_tv);
        forgetPassTv.setOnClickListener(new ForgetPassClickListener());

    }

    class BackClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            finish();
        }
    }

    /**
     * 注册
     *
     * @author zhj
     */
    class RegistClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
            startActivity(intent);
        }
    }

    /**
     * 登录
     *
     * @author zhj
     */
    class LoginClickListener implements OnClickListener {
        @Override
        public void onClick(View view) {
            String account = telEt.getText().toString();
            String pwd = pwdEt.getText().toString();
            if (StringUtil.isEmpty(account)) {
                Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                return;
            }
            if (StringUtil.isEmpty(pwd)) {
                Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!PhoneUtil.isMobileNO(account)) {
                Toast.makeText(LoginActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            }
            Map<String, Object> mapParam = new HashMap<String, Object>();
            mapParam.put("UserAccount", account);
            mapParam.put("Password", pwd);
            mapParam.put("UserAccount", account);
            mapParam.put("DeviceNo", EquipmentUtil.deviceId(LoginActivity.this));
            mapParam.put("Version", EquipmentUtil.getVersionName(LoginActivity.this));
            mapParam.put("DeviceType", "Andriod");
            VolleyManage.getInstance().methodPost(LoginActivity.this, "登录中", ConstantConfig.LOGIN_USER_URL, mapParam,
                    new LoginVolleyReal());
        }
    }

    class LoginVolleyReal implements VolleyInterface {

        @Override
        public void gainData(VolleyBean bean) {
            if (bean.isSuccess()) {
                boolean result = UserController.getInstance(LoginActivity.this).handUser(bean.getContent());
                if (result) {
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(LoginActivity.this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 密码是否明文转换
     *
     * @author zhj
     */
    class PassWordOnCheckedChangeListener implements OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            if (isChecked) {
                pwdEt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                Editable etable = pwdEt.getText();
                Selection.setSelection(etable, etable.length());
            } else {
                pwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                Editable etable = pwdEt.getText();
                Selection.setSelection(etable, etable.length());
            }
        }
    }

    class ForgetPassClickListener implements OnClickListener {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(LoginActivity.this,
                    ForgetPassWordActivity.class);
            startActivity(intent);
        }

    }
}
